#!/usr/bin/env bash
# {argcomplete_hint}

BASE_DIR={base_dir}
DIR={base_dir}

# OSX is not Linux.  To get date to give nanoseconds, we must use gnu date
if [[ $(uname) == "Darwin" ]]
then
    DATE_CMD="/opt/facebook/coreutils/date"
    MV_CMD="/opt/facebook/coreutils/mv"
else
    DATE_CMD="date"
fi
export PAR_LAUNCH_TIMESTAMP=$(${{DATE_CMD}} +%s.%N)
if [ -n "${{{lib_path_env}+SET}}" ]; then
    export FB_SAVED_{lib_path_env}=${lib_path_env}
fi
export {lib_path_env}={ld_library_path}
if [ -n "${{PYTHONPATH+SET}}" ]; then
    export FB_SAVED_PYTHONPATH=$PYTHONPATH
fi

# The following expands to ":PAR_APPEND_PYTHONPATH" when $PAR_APPEND_PYTHONPATH is set
# This is important: we don't want a trailing colon in $PYTHONPATH.
export PYTHONPATH=$BASE_DIR${{PAR_APPEND_PYTHONPATH:+:$PAR_APPEND_PYTHONPATH}}
if [ -n "${{PYTHONHOME+SET}}" ]; then
    export FB_SAVED_PYTHONHOME=$PYTHONHOME
fi
if [ -n "${{{lib_preload_env}+SET}}" ]; then
    export FB_SAVED_{lib_preload_env}=${lib_preload_env}
fi
unset PYTHONHOME
export FB_PAR_MAIN_MODULE="{main_module}"
export FB_PAR_MAIN_FUNCTION="{main_function}"
export FB_PAR_MAIN_RUNNER_MODULE="{main_runner_module}"
export FB_PAR_MAIN_RUNNER_FUNCTION="{main_runner_function}"
export FB_PAR_RUNTIME_FILES=$BASE_DIR
: ${{FB_LPAR_INVOKED_NAME:="$0"}}
export FB_LPAR_INVOKED_NAME
# This environment variable is immediately unset on startup but will also appear
# in e.g. `multiprocessing` workers, and so serves as an audit trail back to
# the originating PAR (and can be read via e.g. `/proc/<pid>/environ`).
export PAR_INVOKED_NAME_TAG="$FB_LPAR_INVOKED_NAME"
{ld_preload}
{env}
exec {cmd} "$@"
